package cn.edu.stu.syllabus.adapters;

import android.graphics.drawable.GradientDrawable;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.edu.stu.syllabus.R;
import cn.edu.stu.syllabus.application.SyllabusApplication;
import cn.edu.stu.syllabus.base.BaseQuickClickAdapter;
import cn.edu.stu.syllabus.repository.takeout.local.TakeOutInfoEntry;
import cn.edu.stu.syllabus.utils.ColorMapper;
import cn.edu.stu.syllabus.widgets.PhoneDialog;

/**
 * Created by daidaijie on 2017/4/2.
 */

public class TakeOutAdapter extends BaseQuickClickAdapter<TakeOutInfoEntry, TakeOutAdapter.ViewHolder> {

    private ColorMapper mColorMapper;

    @Override
    protected ViewHolder createBaseViewHolder(View view) {
        return new ViewHolder(view);
    }

    public TakeOutAdapter(List<TakeOutInfoEntry> data) {
        super(R.layout.item_take_out, data);
        mColorMapper = new ColorMapper(TakeOutInfoEntry.class.getSimpleName());
    }

    @Override
    protected void convert(final ViewHolder helper, final TakeOutInfoEntry item) {

        GradientDrawable shape = (GradientDrawable) ContextCompat
                .getDrawable(SyllabusApplication.getContext(), R.drawable.bg_theme_picker);
        shape.setColor(mColorMapper.getTypeColor(item.getObjectId()));
        helper.mTakeOutShortNameTextView.setBackground(shape);

        helper.mTakeOutShortNameTextView.setText(item.getShortName());
        helper.mTakeOutNameTextView.setText(Html.fromHtml("<b>" + item.getName() + "</b>"));


        helper.mTakeOutNumberTextView.setText(item.getNumberWithTitle());

        helper.mConditionTextView.setText(item.getConditionWithTitle());

        helper.mPhoneImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PhoneDialog.createCallPhoneDialog(
                        helper.mPhoneImageView.getContext(), item.getPhoneList()
                ).show();
            }
        });

        helper.mPhoneImageView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                PhoneDialog.createCopyPhoneNumberDialog(
                        helper.mPhoneImageView.getContext(), item.getPhoneList()
                ).show();

                return true;
            }
        });

        helper.mTakeOutItemLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(item, helper.getAdapterPosition());
                }
            }
        });

    }

    public class ViewHolder extends BaseViewHolder {

        @Nullable
        @BindView(R.id.takeOutItemLayout)
        RelativeLayout mTakeOutItemLayout;
        @Nullable
        @BindView(R.id.takeOutShortNameTextView)
        TextView mTakeOutShortNameTextView;
        @Nullable
        @BindView(R.id.phoneImageView)
        ImageView mPhoneImageView;
        @Nullable
        @BindView(R.id.takeOutNameTextView)
        TextView mTakeOutNameTextView;
        @Nullable
        @BindView(R.id.TakeOutNumberTextView)
        TextView mTakeOutNumberTextView;
        @Nullable
        @BindView(R.id.conditionTextView)
        TextView mConditionTextView;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
